package cn.cs.service.impl;

import cn.cs.client.AgentInvoiceClient;
import cn.cs.client.AgentLevelbPayClient;
import cn.cs.client.SysDtAreaClient;
import cn.cs.common.Constants;
import cn.cs.common.EmptyUtils;
import cn.cs.common.Page;
import cn.cs.pojo.AgentInvoice;
import cn.cs.pojo.AgentLevelbPay;
import cn.cs.service.AgentLevelbPayService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class AgentLevelbPayServiceImpl implements AgentLevelbPayService {


    @Resource
    private AgentLevelbPayClient agentLevelbPayClient;

    @Resource
    private AgentInvoiceClient agentInvoiceClient;

    @Resource
    private SysDtAreaClient sysDtAreaClient;


    public AgentLevelbPay getAgentLevelbPayById(String id) throws Exception {
        return agentLevelbPayClient.getAgentLevelbPayById(id);
    }

    public List<AgentLevelbPay> getAgentLevelbPayListByMap(Map<String, Object> param) throws Exception {
        return agentLevelbPayClient.getAgentLevelbPayListByMap(param);
    }

    public Integer getAgentLevelbPayCountByMap(Map<String, Object> param) throws Exception {
        return agentLevelbPayClient.getAgentLevelbPayCountByMap(param);
    }

    public Integer addAgentLevelbPay(AgentLevelbPay agentLevelbPay) throws Exception {
        return agentLevelbPayClient.addAgentLevelbPay(agentLevelbPay);
    }

    public Integer modifyAgentLevelbPay(AgentLevelbPay agentLevelbPay) throws Exception {
        return agentLevelbPayClient.modifyAgentLevelbPay(agentLevelbPay);
    }

    public Integer deleteAgentLevelbPayById(String id) throws Exception {
        return agentLevelbPayClient.deleteAgentLevelbPayById(id);
    }

    public Page<AgentLevelbPay> queryAgentLevelbPayPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = agentLevelbPayClient.getAgentLevelbPayCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<AgentLevelbPay> agentLevelbPayList = agentLevelbPayClient.getAgentLevelbPayListByMap(param);
        for(AgentLevelbPay a:agentLevelbPayList){
            String orderNum = a.getOrderNum();
            String agent_area = a.getAgent_area();
            AgentInvoice agentInvoice = agentInvoiceClient.getAgentInvoiceByOrderNum(orderNum);
            a.setInvoiceId(agentInvoice.getId());
            a.setType(agentInvoice.getType());
            a.setTitle(agentInvoice.getTitle());
            a.setPhone(agentInvoice.getPhone());
            a.setEmail(agentInvoice.getEmail());
            a.setEinvoice_img_url(agentInvoice.getEinvoiceImgUrl());

            Map<String, Object> map = sysDtAreaClient.getCityNameByAreaId(agent_area);
            a.setProvinceName(map.get("provinceName").toString());
            a.setCityName(map.get("cityName").toString());
            a.setAreaName(map.get("areaName").toString());

        }
        page.setRows(agentLevelbPayList);
        return page;
    }

    public Integer modifyAgentInvoice(AgentInvoice agentInvoice) throws Exception{
        return agentLevelbPayClient.modifyAgentInvoice(agentInvoice);
    }

}
